// IPLayer.h: interface for the CEthernetLayer class.
//
//////////////////////////////////////////////////////////////////////

#pragma once
#include "BaseLayer.h"
#include "ARPLayer.h"
#include "RouterTable.h"
#include "ARPTable.h"

class CIPLayer 
: public CBaseLayer  
{
private:
	inline void		ResetHeader( );
	unsigned char	srcip[4];
	unsigned char	destip[4];

public:
	CIPLayer( char* pName );
	virtual ~CIPLayer();

	u_short checksum(u_short* data,int len);

	void SetSrcIPAddress(unsigned char* src_ip);
	void SetDstIPAddress(unsigned char* dst_ip);
	void SetFragOff(unsigned short fragoff);

	BOOL Send(unsigned char* ppayload, int nlength);
	BOOL Receive(unsigned char* ppayload);

	typedef struct _IPLayer_HEADER {
		unsigned char ip_verlen;	// ip version & header length (1byte)
		unsigned char ip_tos;		// type of service	(1byte)
		unsigned short ip_len;		// total packet length	(2byte)
		unsigned short ip_id;		// datagram id			(2byte)
		unsigned short ip_fragoff;	// fragment offset		(2byte)
		unsigned char ip_ttl;		// time to live in gateway hops (1byte)
		unsigned char ip_proto;		// IP protocol			(1byte)
		unsigned short ip_cksum;	// header checksum		(2byte)
		unsigned char ip_src[4];	// IP address of source	(4byte)
		unsigned char ip_dst[4];	// IP address of destination	(4byte)
		unsigned char ip_data[IP_DATA_SIZE]; // variable length data
	} IPLayer_HEADER, *PIPLayer_HEADER ;

	typedef struct _ICMPLayer_HEADER {
		unsigned char	icmp_type;
		unsigned char	icmp_code;
		unsigned short	icmp_cksum;
		unsigned short	icmp_id;
		unsigned short	icmp_seq;
		unsigned char	icmp_data[ICMP_DATA_SIZE];
	}ICMPLayer_HEADER, *PICMPLayer_HEADER ;

	CRouterTable*	m_RoutTable;
	CARPTable*		m_ARPTable;

	void	SetARPLayerObject(CARPLayer* arp);
	void	SetExARPLayerObject(CARPLayer* exarp);
	CARPLayer*		m_ARP;
	CARPLayer*		m_ExARP;

	void	SetInnerIPAddress(unsigned char* ip);
	void	SetExternIPAddress(unsigned char* ip);
	unsigned char in_ip[4];
	unsigned char ex_ip[4];

	unsigned char** ipmsgQ;
	int front,rear;
	void	IPMessageQ_Init();
	void	IPMessageQ_Free();
	void	IPMessageQ_Insert(unsigned char* ppayload,int length);
	unsigned char*	IPMessageQ_Search(unsigned char* ppayload);
	
protected:
	IPLayer_HEADER	m_sHeader ;
	ICMPLayer_HEADER m_sData;
};
